/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package com.android1.amarena2d.input;

import com.android1.amarena2d.commons.Temp;
import com.android1.amarena2d.engine.AmarenaCamera;
import com.android1.amarena2d.engine.TouchSpotSystem;
import com.android1.amarena2d.nodes.behavior.Touchable;
import com.android1.amarena2d.nodes.behavior.delegates.VolumneDelegate;


public class EntityTouchSpot extends BaseTouchSpot implements TouchSpot {

    private Touchable entity;
    private boolean hasVolumneOffset;
    private AmarenaCamera camera;

    public EntityTouchSpot(Touchable entity) {
        this.entity = entity;
        this.hasVolumneOffset = entity.hasVolumne();
        this.priority = TouchSpotSystem.nextPriority();

    }

    public EntityTouchSpot(Touchable entity, int priority) {
        this(entity);
        this.priority = priority;
    }

    @Override
    public boolean isHit(final float x, final float y) {
        entity.translateToLocalCoords(Temp.Vector.set(x, y));

        if (!hasVolumneOffset)
            return !(Temp.Vector.x < 0 || Temp.Vector.x > entity.getWidth()
                    || Temp.Vector.y < 0 || Temp.Vector.y > entity.getHeight());
        else {
            final VolumneDelegate v = entity.volumne();
            return !(Temp.Vector.x < 0 + v.xyOffset.x || Temp.Vector.x > v.xyOffset.x + v.size.width
                    || Temp.Vector.y < 0 + v.xyOffset.y || Temp.Vector.y > v.xyOffset.y + v.size.height);
        }
    }


    @Override
    public boolean isActive() {
        return entity.isActive();
    }

    @Override
    public AmarenaCamera getCamera() {
        if (camera == null)
            camera = entity.getCamera();
        return camera;
    }
}